package view.rules;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import view.rules.components.FeaturePanel;

public class FeatureButtonListener implements ActionListener{
	
	FeaturePanel featurePanel;
	JDialog featureButtonDialog;
	private JPanel firstPanel;
	private JPanel secondPanel;
	public JButton okButton;
	public JButton cancelButton;
	private String pos;
	FeaturePane featurePane;
	FeaturePanel panel;
	Map<String,ArrayList<String>> selectedValues;


	@Override
	public void actionPerformed(ActionEvent e) {
		initComponents();

		
	}

	public void initComponents(){
		firstPanel = new JPanel();
		firstPanel.setLayout(new MigLayout());
		
		featurePanel = new FeaturePanel();
		okButtonActionListener okListener = new okButtonActionListener(featurePanel);
		
		secondPanel = new JPanel();
		okButton = new JButton("OK");
		cancelButton = new JButton("Cancel");
		secondPanel.add(okButton);
		secondPanel.add(cancelButton);
        
		okButton.addActionListener(okListener);
		okButton.setActionCommand("OK");
		cancelButton.addActionListener(okListener);
		cancelButton.setActionCommand("Cancel");
		
		okListener.setDialogToClose(featureButtonDialog);
		
		selectedValues = new HashMap<String,ArrayList<String>>();
	}

	
	public void setPOS(String pos){
		this.pos = pos;
		System.out.println("In the listener " + this.pos);
		
		featureButtonDialog = new JDialog();
		featureButtonDialog.setTitle("Specify a Start or an End Verse");
		featureButtonDialog.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
		featureButtonDialog.setSize(500, 500);
		featureButtonDialog.setModal(true);
		featureButtonDialog.setLocationRelativeTo(null);
		
		panel = new FeaturePanel(pos);
		okButtonActionListener okListener = new okButtonActionListener(featurePanel);
		firstPanel.remove(featurePanel);
		firstPanel.add(panel, "wrap");
		firstPanel.add(secondPanel);
		
		featureButtonDialog.add(firstPanel);
		featureButtonDialog.setResizable(false);
		okListener.setDialogToClose(featureButtonDialog);
		okButton.addActionListener(okListener);
		okButton.setActionCommand("OK");
		cancelButton.addActionListener(okListener);
		cancelButton.setActionCommand("Cancel");
		featureButtonDialog.setVisible(true);
	
	}
	
	
	private class okButtonActionListener implements ActionListener {
        private JPanel source;
        private JDialog dialog;
        
        public okButtonActionListener(JPanel source) {
            this.source = source;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        	
            if (e.getActionCommand().equals("OK")) {
            	selectedValues = panel.getSelected();
                closeDialog();
            } else if (e.getActionCommand().equals("Cancel")) {
                closeDialog();
            }
            
        }
        
        private void closeDialog() {
            if (dialog != null) {
                dialog.dispose();
            }
        }
        
        public void setDialogToClose(JDialog dialog) {
            this.dialog = dialog;
        }
    
    }
	
	public Map<String,ArrayList<String>>  getFeatures(){
		return this.selectedValues;
	}

}
